package com.guangjian.hb.common;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.guangjian.hb.domain.User;

public final class HibernateUtil {
    private static SessionFactory sf = null;
    private static final Logger log = Logger.getLogger(HibernateUtil.class);
    private static final ThreadLocal sessions = new ThreadLocal();

    static {
        Configuration cfg = new Configuration();
        cfg.configure();
        sf = cfg.buildSessionFactory();
    }

    public static Session currentSession() {
        Session session = (Session) sessions.get();
        if (session == null) {
            session = sf.openSession();
            sessions.set(session);
        }
        return session;
    }

    public static void closeSession(Session session) {
        session.close();
        sessions.set(null);
    }

    // test method
    public static void main(String[] args) {

        firstQuery();
    }

    private static void firstQuery() {

        Session session = null;
        try {
            session = currentSession();
            log.debug("---------------- firstQuery -----------------START");
            Criteria criteria = session.createCriteria(User.class);
            criteria.add(Restrictions.like("name", "%j%"));
            List<User> users = (List<User>) criteria.list();
            for (User user : users) {
                log.debug(user.getName());
            }

            log.debug("---------------- firstQuery -----------------END");
            
            log.debug("---------------- secondQuery -----------------START");

            Criteria criteria2 = session.createCriteria(User.class);
            criteria2.add(Restrictions.like("name", "%j%"));
            List<User> users2 = (List<User>) criteria2.list();
            for (User user : users) {
                log.debug(user.getName());
            }

            log.debug("---------------- secondQuery -----------------END");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (session != null)
                closeSession(session);
        }

    }

    private static void secondQuery() {
        log.debug("---------------- secondQuery -----------------START");

        Session session = null;
        try {
            session = currentSession();
            Criteria criteria = session.createCriteria(User.class);
            criteria.add(Restrictions.like("name", "%j%"));
            List<User> users = (List<User>) criteria.list();
            for (User user : users) {
                log.debug(user.getName());
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (session != null)
                closeSession(session);
        }

        log.debug("---------------- secondQuery -----------------END");
    }

}
